//app.js
App({
  onLaunch: function () {
    var that = this;
    //  获取商城名称
    wx.request({
      url: that.globalData.domain + '/config/global',
      method: "GET",
      success: function (res) {
        if (res.data.code == 200) {
          wx.setStorageSync("mallName", res.data.data.mallName);
        }
      }
    })
    this.login();
  },
  login: function () {
    var that = this;
    var token = that.globalData.token;
    //判断用户是否登录过 如果登录后台获取信息
    if (token) {
      wx.request({
        url: that.globalData.domain + '/user/token',
        data: {
          token: token
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: "POST",
        success: function (res) {
          if (res.data.code == 201) {
            that.globalData.token = null;
            that.login();
          }
        }
      })
      return;
    }

    //当前用户未登录登录后查询
    wx.login({
      success: function (res) {
        wx.request({
          url: that.globalData.domain + '/user/thirdParty/login',
          data: {
            code: res.code,
            type:'wxapp'
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: "POST",
          success: function (res) {
            if (res.data.code == 1801) {
              // 去注册
              that.registerUser();
              return;
            }
            if (res.data.code != 200) {
              // 登录错误
              wx.hideLoading();
              wx.showModal({
                title: '提示',
                content: '无法登录，请重试',
                showCancel: false
              })
              return;
            }
            that.globalData.token = res.data.data.token;
            that.globalData.uid = res.data.data.id;
          }
        })
      }
    })
  },
  registerUser: function () {
    var that = this;
    wx.login({
      success: function (res) {
        var code = res.code; // 微信登录接口返回的 code 参数，下面注册接口需要用到
        wx.getUserInfo({
          success: function (res) {
            console.info(res);
            var iv = res.iv;
            var encryptedData = res.encryptedData;
            // 下面开始调用注册接口
            wx.request({
              url: that.globalData.domain + '/user/thirdParty/register',
              data: { code: code, encryptedData: encryptedData, iv: iv,type:'wxapp'}, // 设置请求的 参数
              method: "POST",
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: (res) => {
                wx.hideLoading();
                that.login();
              }
            })
          }
        })
      }
    })
  },






  sendTempleMsg: function (orderId, trigger, template_id, form_id, page, postJsonString) {
    var that = this;
    wx.request({
      url: 'https://api.it120.cc/' + that.globalData.subDomain + '/template-msg/put',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        token: that.globalData.token,
        type: 0,
        module: 'order',
        business_id: orderId,
        trigger: trigger,
        template_id: template_id,
        form_id: form_id,
        url: page,
        postJsonString: postJsonString
      },
      success: (res) => {
        
      }
    })
  },
  sendTempleMsgImmediately: function (template_id, form_id, page, postJsonString) {
    var that = this;
    wx.request({
      url: 'https://api.it120.cc/' + that.globalData.subDomain + '/template-msg/put',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        token: that.globalData.token,
        type: 0,
        module: 'immediately',
        template_id: template_id,
        form_id: form_id,
        url: page,
        postJsonString: postJsonString
      },
      success: (res) => {
        console.log(res.data);
      }
    })
  },
  getUserInfo: function (cb) {
    var that = this
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登陆接口
      wx.login({
        success: function () {
          wx.getUserInfo({
            success: function (res) {
              that.globalData.userInfo = res.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      })
    }

  },
  globalData: {
    userInfo: null,
    domain: "http://www.jiaonainai.cn",
    subDomain: "tz",
    version: "0.0.1-SNAPSHOT",
    //分享信息
    shareProfile: '百款精品商品，总有一款适合您'
  }
})
